<?php

namespace App\Http\Controllers\Admin;

use App\Http\Common\Lib\Common;
use App\Models\AdminUser;
use App\Models\LoginRecord;
use App\Models\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminController extends BaseAdminController
{
	//
	private $model;

	public function __construct()
	{
		parent::__construct();
		$this->model = new AdminUser();
	}

	public function index()
	{
		if (\request()->ajax()) {
			$page = \request()->input('page', 1);
			$pageSize = \request()->input('pageSize', 20);
			$search = \request()->input('search', '');
			$where = [
				['and' => ['status', '>', -1]],
			];
			if ($search) {
				$where[] = ['like' => ['account|nickname|tel', '%' . $search . '%']];
			}
			$field = ['id', 'account', 'name', 'nickname', 'sex', 'tel', 'email', 'created_at', 'status'];
			$pageData = $this->model->getPages($page, $pageSize, $where, $field, ['created_at' => 'asc']);
			return response()->json(['status' => $pageData['status'], 'msg' => $pageData['msg'], 'page' => $page, 'totalCount' => $pageData['data']['totalCount'], 'pages' => $pageData['data']['pages']]);

		} else {
			return view('admin.admin.index');
		}
	}

	/**
	 * 添加页面
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
	 */
	public function add()
	{
		return view('admin.admin.add');
	}

	/**
	 * 保存添加
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function addStore()
	{
		$input = \request()->all();
		$res = $this->model->add($input);
		return response()->json(Common::dataFormat($res['status'], $res['msg'], $res['data']));
	}

	/**
	 * 添加页面
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
	 */
	public function edit()
	{
		return view('admin.admin.edit');
	}

	/**
	 * 保存添加
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function editStore()
	{
		$input = \request()->all();
		$res = $this->model->edit($input);
		return response()->json(Common::dataFormat($res['status'], $res['msg'], $res['data']));
	}

	/**
	 * 修改状态
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function changeStatus()
	{
		$id = \request()->input('id', 0);
		$opt = \request()->input('opt', '');
		$res = $this->model->changeStatus($id, $opt);
		return response()->json(Common::dataFormat($res['status'], $res['msg'], $res['data']));
	}

	/**
	 * 详情
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function detail()
	{
		$id = \request()->input('id', 0);
		$res = $this->model->findOne($id, ['id', 'account', 'nickname', 'tel', 'name', 'email', 'status', 'sex', 'avatar']);
		return response()->json(Common::dataFormat($res['status'], $res['msg'], $res['data']));
	}

	/**
	 * 角色设置页面
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
	 */
	public function role()
	{
		return view('admin.admin.role');
	}

	/**
	 * 保存角色设置
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function roleStore()
	{
		$id = \request()->input('id', 0);
		$roleIds = \request()->input('roleIds', []);
		$res = $this->model->saveRoles($id, $roleIds);
		return response()->json(Common::dataFormat($res['status'], $res['msg']));
	}

	/**
	 * 管理员的角色列表
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function roleInfo()
	{
		$id = \request()->input('id', 0);
		$userInfo = $this->model->findOne($id);
		if ($userInfo['status'] != config('statusInfo.status.success')) {
			return response()->json(Common::dataFormat($userInfo['status'], $userInfo['msg']));
		}
		$user = $userInfo['data'];
		$userRoles = $user->roles;
		$roleIds = $userRoles->pluck('id')->toArray();
		$roleModel = new Role();
		$roles = $roleModel->treeList(0, [['status', '=', 1]], true, $roleIds);

		return response()->json(Common::dataFormat(config('statusInfo.status.success'), config('statusInfo.msg.success'), ['roleIds' => $roleIds, 'roles' => $roles]));
	}

	/**
	 * 管理员登陆记录
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View|\think\response\View
	 */
	public function loginRecord()
	{
		if (\request()->ajax()) {
			$page = \request()->input('page', 1);
			$pageSize = \request()->input('pageSize', 20);
			$id = \request()->input('id', 0);
			$where = [
				['and' => ['user_id', '=', $id]],
				['and' => ['type', '=', 2]],
			];
			$order = [
				'login_time' => 'desc',
			];
			$recordModel = new LoginRecord();
			$pageData = $recordModel->getPages($page, $pageSize, $where, '*', $order, 'adminUser');
			if ($pageData['data']['pages']) {
				foreach ($pageData['data']['pages'] as $value) {
					$value->user_account = '';
					if ($value->adminUser) {
						$value->user_account = $value->adminUser->account;
					}
					unset($value->adminUser);
					$value->login_time = date('Y-m-d H:i:s', $value->login_time);
				}
			}
			return response()->json(['status' => $pageData['status'], 'msg' => $pageData['msg'], 'page' => $page, 'totalCount' => $pageData['data']['totalCount'], 'pages' => $pageData['data']['pages']]);

		} else {
			return view('admin.admin.record');
		}
	}
}
